package me.jessyan.armscomponent.app.mvp.presenter.user;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.annotations.NonNull;
import me.jessyan.armscomponent.app.mvp.contract.user.UserTestKnowledgeLevelContract;
import me.jessyan.armscomponent.app.mvp.model.entity.Question;
import me.jessyan.armscomponent.app.mvp.model.entity.QuestionRequest;
import me.jessyan.armscomponent.app.mvp.model.entity.QuestionSubmitRequest;
import me.jessyan.armscomponent.commonsdk.http.BaseResponse;
import me.jessyan.armscomponent.commonsdk.utils.RxUtil;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/19/2021 23:20
 * Description: describe of this class
 */
@ActivityScope
public class UserTestKnowledgeLevelPresenter extends BasePresenter<UserTestKnowledgeLevelContract.Model, UserTestKnowledgeLevelContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    List<Question> mDatas;
    @Inject
    RecyclerView.Adapter mAdapter;

    @Inject
    public UserTestKnowledgeLevelPresenter(UserTestKnowledgeLevelContract.Model model, UserTestKnowledgeLevelContract.View rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    void onCreate() {
        getQuestionList();
    }

    public void getQuestionList(){
        mModel.getQuestionList()
                .compose(RxUtil.io_main())
                .doOnSubscribe(disposable -> {
                    addDispose(disposable);
                    mRootView.showLoading();
                })
                .doFinally(() -> mRootView.hideLoading())
//                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<List<Question>>>(mErrorHandler) {
                    @Override
                    public void onNext(@NonNull BaseResponse<List<Question>> listBaseResponse) {
                        if(listBaseResponse.success()){
                            mDatas.addAll(listBaseResponse.getResult());
                            mAdapter.notifyDataSetChanged();
                        }else {
                            BaseResponse.Errmsg errmsg = listBaseResponse.getErrmsg();
                            if(null != errmsg){
                                mRootView.showMessage(errmsg.getEn());
                            }else{
                                mRootView.showMessage("接口返回参数异常");
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable t) {
                        super.onError(t);
                        mRootView.showMessage("获取测试答卷异常,"+t.getMessage());
                    }
                });
    }

    public void putQuestionList(List<Question> answers){
        List<QuestionRequest> requests = new ArrayList<>();
        QuestionSubmitRequest submitRequest = new QuestionSubmitRequest();
        for (int i = 0; i < answers.size(); i++) {
            requests.add(QuestionRequest.convert(answers.get(i)));
        }
        submitRequest.setAnswers(requests);
        mModel.putQuestionList(submitRequest)
                .compose(RxUtil.io_main())
                .doOnSubscribe(disposable -> {
                    addDispose(disposable);
                    mRootView.showLoading();
                })
                .doFinally(() -> mRootView.hideLoading())
//                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .subscribe(new ErrorHandleSubscriber<BaseResponse<Object>>(mErrorHandler) {
                    @Override
                    public void onNext(@NonNull BaseResponse<Object> listBaseResponse) {
                        if(listBaseResponse.success()){
                            mRootView.onSubmitSuccess();
                        }else {
                            BaseResponse.Errmsg errmsg = listBaseResponse.getErrmsg();
                            if(null != errmsg){
                                mRootView.showMessage(errmsg.getEn());
                            }else{
                                mRootView.showMessage("接口返回参数异常");
                            }
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable t) {
                        super.onError(t);
                        mRootView.showMessage("提交测试答卷异常,"+t.getMessage());
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mApplication = null;
    }
}
